package com.qf.controller;

import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsRole;
import com.qf.service.DtsPermissionService;
import com.qf.service.DtsRoleService;
import com.qf.util.Permission;
import com.qf.util.PermissionUtil;
import com.qf.util.ResponseUtil;
import com.qf.vo.PermVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 角色管理
 * @author bss
 * @version V1.0
 * @Project maven
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/10/14 14:11
 */
@RestController
@RequestMapping("/admin/role")
@Slf4j
public class AdminRoleController {

    @Autowired
    DtsRoleService roleService;
    @Autowired
    ApplicationContext context;
    @Autowired
    DtsPermissionService permissionService;

    @RequestMapping("/options")
    public Object options(){
        List<DtsRole> roleList = roleService.findAll();
        List<Map<String, Object>> data = roleList.stream().map(dtsRole -> {
            Map<String, Object> map = new HashMap<>();
            map.put("label", dtsRole.getName());
            map.put("value", dtsRole.getId());
            return map;
        }).collect(Collectors.toList());

        return ResponseUtil.ok(data);
    }

    @RequiresPermissions("admin:role:list")
    @RequiresPermissionsDesc(menu = { "系统管理", "角色管理" }, button = "角色查询")
    @GetMapping("/list")
    public Object list(@RequestParam(value = "page",required = false,defaultValue = "1") Integer page,
                       @RequestParam(value = "limit",required = false,defaultValue = "20")Integer limit,
                       @RequestParam(value = "sort",required = false,defaultValue = "add_time")String sort,
                       @RequestParam(value = "order",required = false,defaultValue = "desc")String order,
                       String rolename){

        Map<String, Object> data = roleService.findByPage(page,limit,sort,order,rolename);
        return ResponseUtil.ok(data);
    }

    @RequiresPermissions("admin:role:create")
    @RequiresPermissionsDesc(menu = { "系统管理", "角色管理" }, button = "角色添加")
    @PostMapping("/create")
    public Object create(@RequestBody DtsRole role){
        roleService.insert(role);
        return ResponseUtil.ok(role);
    }


    @RequiresPermissions("admin:role:update")
    @RequiresPermissionsDesc(menu = { "系统管理", "角色管理" }, button = "角色编辑")
    @PostMapping("/update")
    public Object update(@RequestBody DtsRole role){
        roleService.update(role);
        return ResponseUtil.ok();
    }

    @RequiresPermissions("admin:role:delete")
    @RequiresPermissionsDesc(menu = { "系统管理", "角色管理" }, button = "角色删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsRole role){
        roleService.delete(role);
        return ResponseUtil.ok();
    }

    @RequiresPermissions("admin:role:permission:get")
    @RequiresPermissionsDesc(menu = { "系统管理", "角色管理" }, button = "权限详情")
    @GetMapping("/permissions")
    public Object permissions(Integer roleId){

//        systemPermissions 系统权限（所有权限）List<PermVo>
        List<Permission> permissionList = PermissionUtil.listPermission(context, "com.qf.controller");
        List<PermVo> systemPermissions = PermissionUtil.listPermVo(permissionList);

//        assignedPermissions  该角色已经有的权限 List<String>
        Set<String> assignedPermissions = null;
//        如果是超级管理员，则拥有所有权限
        boolean flag = roleService.isSuperAdmin(roleId);
        if (flag){
//        获取权限字符串
            assignedPermissions = PermissionUtil.listPermissionString(permissionList);
        }
//        如果不是超级管理员，则查找拥有的权限
        else {
            assignedPermissions = permissionService.findPermissionByRoleid(roleId);
        }

        Map<String, Object> data = new HashMap<>();
        data.put("systemPermissions", systemPermissions);
        data.put("assignedPermissions", assignedPermissions);

        return ResponseUtil.ok(data);
    }

    @PostMapping("/permissions")
    public Object updatePermissions(@RequestBody Map<String, Object> map){
        int roleId = Integer.parseInt(String.valueOf(map.get("roleId")));
        List<String> permissions = (List<String>) map.get("permissions");

        log.debug("permissions list:{}",permissions);
//        如果该角色为超级管理员 设置权限为 *
        if (roleService.isSuperAdmin(roleId)){
            permissions = new ArrayList<>();
            permissions.add("*");
        }
        permissionService.updatePermissions(roleId,permissions);
        return ResponseUtil.ok();
    }
}
